package com.cgsd.heyu.utils;

import com.android.library.utils.TimeUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * @author xiaoxing
 * Description 说明
 * CreateData: 2020/11/27
 */
public class DateUtils {
    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();


    /**
     * 日期差(天)
     * @param time
     * @return
     */
    public static int dateDifference(long time) {
        try {
            long predate =  System.currentTimeMillis();
            long dayTime = predate- (predate + TimeZone.getDefault().getRawOffset()) % (24 * 60 * 60 * 1000);
            long serviceTime =time- (time + TimeZone.getDefault().getRawOffset()) % (24 * 60 * 60 * 1000);
            return (int) ((dayTime-serviceTime)/(24 * 60 * 60 * 1000));
        }catch (Exception e){
            return -1;
        }
    }

    /**
     * 转为今天，昨天，前天，日期
     * @return
     */
    public static String getDayDateFormat(long time) {
        int num = dateDifference(time);
        if (num == 0){
            return "今天";
        }else if (num == 1){
            return "昨天";
        }else if (num == 2){
            return "前天";
        }else {
            Calendar calendar = Calendar.getInstance();
            String systemYear = String.valueOf(calendar.get(Calendar.YEAR));
            String str = TimeUtil.dateString(time/1000,"yyyy-MM-dd");
            String serviceYear = str.substring(0,4);
            if (systemYear.equals(serviceYear)){
                return str.substring(5);
            }else {
                return str;
            }
        }
    }


}
